package com.ruoyi.project.mgcmanage.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgcmanage.domain.MedFactory;
import com.ruoyi.project.mgcmanage.service.IMedFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mgcmanage/producer")
public class MedFactoryController extends BaseController {
    @Autowired
    private IMedFactoryService iMedFactoryService;
    /**
     * 查询生产厂家列表
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:query')")
    @GetMapping("/list")
    public TableDataInfo getList(MedFactory medFactory){
        startPage();

        List<MedFactory> list = iMedFactoryService.showList(medFactory);
        return getDataTable(list);
    }

    /**
     * 添加生产厂家
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:add')")
    @PostMapping
    public AjaxResult addProducer(@RequestBody MedFactory medFactory){
        medFactory.setCreateBy(SecurityUtils.getUsername());
        int row = iMedFactoryService.addProducer(medFactory);
        return toAjax(row);
    }

    /**
     * 通过id查询当前厂家信息，做数据回显，以便修改厂家
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:query')")
    @GetMapping("/{id}")
    public AjaxResult getProducerById(@PathVariable int id){
        System.out.println(id);
       MedFactory medFactory= iMedFactoryService.selectById(id);
       return AjaxResult.success(medFactory);
    }

    /**
     * 修改厂家信息
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:update')")
    @PutMapping
    public AjaxResult updateProducer(@RequestBody MedFactory medFactory){
        medFactory.setUpdateBy(SecurityUtils.getUsername());
        int row = iMedFactoryService.updateProducer(medFactory);
        return toAjax(row);
    }

    /**
     * 删除厂家操作
     * @param facIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:remove')")
    @DeleteMapping("/{facIds}")
    public AjaxResult deleteProducer(@PathVariable int[] facIds){
        return toAjax(iMedFactoryService.deleteById(facIds));
    }

    /**
     *查询厂家名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:producer:query')")
    @GetMapping("/all")
    public AjaxResult selectAll(){
        return AjaxResult.success(iMedFactoryService.selectAll());
    }
}
